package com.tao.controller;

import com.tao.entity.User;
import com.tao.enums.AppHttpCodeEnum;
import com.tao.exception.SystemException;
import com.tao.service.LoginService;
import com.tao.vo.ResponseResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 季伟
 * 功能：
 */
@RestController
public class LoginController {
    @Autowired
    LoginService loginService;

    @RequestMapping("/login")
    public ResponseResult login(@RequestBody User loginUser){

        if (Strings.isEmpty(loginUser.getUserName())){
            throw  new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return loginService.login(loginUser);
    }
    @RequestMapping("/logout")
    public ResponseResult logout(){
        return loginService.logout();
    }
    @RequestMapping("isLogin")
    public ResponseResult isLogin(){
        return ResponseResult.okResult(200,"已登录");
    }

    public static void main(String[] args) {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        System.out.println(passwordEncoder.encode("root"));
        System.out.println(passwordEncoder.matches("kiyato","$2a$10$WD.8q2dGcX71On3WgfzUjOw3X4ltcmlf5ZcXemMOsb6hlh0oL5YjG"));
    }
}
